﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace fastCSharp.threading
{
    /// <summary>
    /// 任务信息
    /// </summary>
    internal struct taskInfo
    {
        /// <summary>
        /// 调用委托
        /// </summary>
        public Action Call;
        /// <summary>
        /// 任务执行出错委托,停止任务参数null
        /// </summary>
        public Action<Exception> OnError;
        /// <summary>
        /// 执行任务
        /// </summary>
        public void Run()
        {
            try
            {
                Call();
            }
            catch (Exception error)
            {
                if (OnError == null) fastCSharp.log.Error.Add(error, null, false);
                else
                {
                    try
                    {
                        OnError(error);
                    }
                    catch (Exception exception)
                    {
                        fastCSharp.log.Error.Add(exception, null, false);
                    }
                }
            }
        }
        /// <summary>
        /// 执行任务
        /// </summary>
        [MethodImpl((MethodImplOptions)fastCSharp.pub.MethodImplOptionsAggressiveInlining)]
        public void RunClear()
        {
            Run();
            Call = null;
            OnError = null;
        }
    }
}
